﻿package com.codingbros.countdown.view {
	
	import com.codingbros.countdown.model.IModel;
	import flash.display.MovieClip;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author peter@codingbros.com
	 */	
	public class View extends MovieClip implements IView
	{
		private var model:IModel;
		private var background:MovieClip;
		
		private var titelText:TextField;
		private var counterText:TextField;
		
		public function View() 
		{
			background = new Background();
			this.addChild(background);
		}
		
		public function init(model:IModel):void {
			this.model = model;
			
			titelText = new TextField();
			TextUtils.setupTextField(titelText, 20, 0x2cbbc4);
			titelText.width = 300;
			titelText.text = model.countdownTitel + " in:";
			titelText.x = background.width / 2 - titelText.textWidth / 2
			titelText.y = background.height / 2 - titelText.textHeight / 2
			this.addChild(titelText);
			
			counterText = new TextField();
			TextUtils.setupTextField(counterText, 20,  0x13757b);
			counterText.width = 300;
			counterText.x = titelText.x
			counterText.y = titelText.y + titelText.textHeight;
			this.addChild(counterText);
		}
		
		public function update():void {
			var timeLeft:Date = new Date(model.countdownEndDate.getTime() - model.currentDate.getTime());
			var days:int = timeLeft.dateUTC;
			var hours:int = timeLeft.hoursUTC;
			var minutes:int = timeLeft.minutesUTC;
			var seconds:int = timeLeft.secondsUTC;
			var milliSeconds:int = timeLeft.millisecondsUTC;
			counterText.text = "t - " + days + ", " + hours + ":" + minutes + ":" + seconds + ":" + milliSeconds;
		}		
	}	
}

